#include "mapdisplay.h"

//mapdisplay---------------------------
//constructs------------
mapScreen::mapScreen() : Gtk::Table(3, 3){
    rep = new mapDisplay();
    rep->set_visible(true);
    attach(*rep, 0, 3, 0, 3);
}
mapScreen::~mapScreen(){
    rep->set_visible(false);
    delete rep;
}
/* active player--------
 * decides which ships are
 * drawn in red and which in
 * green
 *--------------------*/
int mapScreen::activePlayer(){
    return activeP;
}
void mapScreen::activePlayer(int p){
    activeP = p;
}
/* request cursor-------
 * attatches the client
 * to the maps cursor
 * -------------------*/
void mapScreen::requestCursor(mapCursClient* client){
    rep->requestCursor(client);
}
/* overides-------------
 * properly handles the
 * widget's functions
 * -------------------*/
bool mapScreen::on_key_press_event(GdkEventKey* event){
    return rep->keypress(event);
}
bool mapScreen::on_button_press_event(GdkEventButton* event){
    return false;
}
bool mapScreen::on_focus_in_event(GdkEventFocus* event){
    rep->grab_focus();
    return true;
}
//=====================================
//mapRepresentation--------------------
//constructs------------
mapDisplay::mapDisplay(){
    mapDisplay(10, 10, 1);
}
mapDisplay::mapDisplay(int x, int y){
    mapDisplay(x, y, 1);
}
mapDisplay::mapDisplay(int x, int y, int playerNum){
    feck = new tileDisp**[x];
    for(int i = 0; i < x; i++){
        feck[i] = new tileDisp*[y];
        for(int j = 0; j < y; j++){
            feck[i][j] = new tileDisp(i, j);
            feck[i][j]->set_visible(true);
        }
    }
}
mapDisplay::~mapDisplay(){
    for(int i = 0; i < dims[0]; i++){
       for(int j  = 0; j < dims[1]; j++){
           delete feck[i][j];
       }
       delete feck[i];
    }
    delete feck;
}
/* request cursor-------
 * attaches a client to the
 * cursor.  signaled when
 * enter presed or lmb
 * click
 * -------------------*/
void mapDisplay::requestCursor(mapCursClient* client){
    cursSig.connect(sigc::mem_fun(client, &mapCursClient::recieveCursor));
}
/* keypress-------------
 * act on an external
 * keypress
 * -------------------*/
bool mapDisplay::keypress(GdkEventKey* btn){
    return on_key_press_event(btn);
}
/* draw-----------------
 * draw the given bs
 * -------------------*/
void mapDisplay::draw(){

}
/* overides-------------
 * rewrite the widget
 * functions
 * -------------------*/
bool mapDisplay::on_expose_event(GdkEventExpose* event){
    draw();
}
bool mapDisplay::on_button_press_event(GdkEventButton* event){
    
}
bool mapDisplay::on_key_press_event(GdkEventKey* event){
    feck[cursor[0]][cursor[1]]->curs(false);
    switch(event->keyval){
        case GDK_Left:
            if((--cursor[0]) < 0)
                cursor[0]++;
            break;
        case GDK_Right:
            if((++cursor[0]) >= dims[0])
                cursor[0]--;
            break;
        case GDK_Up:
            if((--cursor[1]) < 0)
                cursor[1]++;
            break;
        case GDK_Down:
            if((++cursor[1]) >= dims[1])
                cursor[1]--;
            break;
        case GDK_Return:
            feck[cursor[0]][cursor[1]]->curs(true);
            cursSig.emit(cursor[0], cursor[1]);
            break;
        default:
            feck[cursor[0]][cursor[1]]->curs(true);
            return false;
            break;
    }
    feck[cursor[0]][cursor[1]]->curs(true);
    scroll();
    return true;
}
/* scroll---------------
 * scroll the screen if
 * the cursor leaves the
 * edge
 * -------------------*/
void mapDisplay::scroll(){
    int offRight[2], offLeft[2];
    offRight[0] = offset[0] + dims[0] - cursor[0];
    offRight[1] = offset[1] + dims[1] - cursor[1];
    offLeft[0]  = offset[0] - cursor[0];
    offLeft[1]  = offset[1] - cursor[1];
    if(offRight[0] > 0){
        offset[0]++;
    } else if (offLeft[0] < 0){
        offset[0]--;
    }
    if(offRight[1] > 0){
        offset[1]++;
    } else if (offLeft[1] < 0){
        offset[1]--;
    }
}
//=====================================
//tileDisp-----------------------------
//constructs------------
tileDisp::tileDisp(){
    tileDisp(0,0);
}
tileDisp::tileDisp(int _x, int _y){
    x = _x;
    y = _y;
    icn = new iconContainer();
}
tileDisp::~tileDisp(){
    icn->set_visible(false);
    delete icn;
}
//=====================================